package ajjj.kiviq.beans;

import ajjj.kiviq.Constants;
import ajjj.kiviq.entities.UserEnt;
import ajjj.kiviq.remoteInterfaces.IRemoteCategory;
import ajjj.kiviq.remoteInterfaces.IRemoteUser;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *  Updated 13-10-11 by jeklund
 *  Added the method currentUser()
 * 
 * @author orjohan
 */
@ManagedBean(name = "UserBean")
@SessionScoped
public class UserBean {
    
    @EJB
    private IRemoteCategory cf; //Used when removing a UserEnt
    @EJB
    private IRemoteUser uf;

    /** Creates a new instance of UserBean */
    public UserBean() {
    }
    
    public String removeUser(){
        
	String user = currentUser();
        UserEnt ue = uf.findUser(user);
        cf.removeCategories(user);
        uf.updateUser(ue);
        uf.removeUser(ue);

        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(Constants.User);
    
        return "login";
    }
   
    /**
     * retrieves the current users name as a string
     * 
     * @return user name 
     */
    public String currentUser() {
        return (String) FacesContext.getCurrentInstance().getExternalContext()
                .getSessionMap().get(Constants.User);
    }
}
